leagueoflegendsfandomcom_it-20200214-history
Modulo:Hextech
--@Documentation: Template:Hextech --@Author: User:Nanaki local getArgs = require('Dev:Arguments').getArgs local skins = { 390 = 3, 460 = 1, 520 = 162, 750 = 128, 975 = 282, 1350 = 180, 1820 = 37, 2450 = 6, 2775 = 1, 3250 = 4, } local reroll_threshold = 2450 local function formula(avg, a, b, c) a = (a >= reroll_threshold and 0.5 or 0.2) * a b = (b >= reroll_threshold and 0.5 or 0.2) * b c = (c >= reroll_threshold and 0.5 or 0.2) * c return avg - 300 - a - b - c end -- Util local function keys(tab, sorted) local keys = {} for k,v in pairs(tab) do table.insert(keys, k) end if sorted then table.sort(keys) end return keys end local function copytable(tab) local new = {} for k,v in pairs(tab) do newk = v end return new end -- Display functions local function summary_table(heading, values) local t = mw.html.create('table'):addClass('article-table'):css('width', '400px') local total, rp = 0, 0 for k,v in pairs(values) do total = total + v rp = rp + k*v end t:tag('tr') :tag('th') :wikitext(heading) :attr('colspan', 2) t:tag('tr') :tag('th') :wikitext('Total:') :attr('scope', 'row') :css('width', '100px') :done() :tag('td') :wikitext(total) t:tag('tr') :tag('th') :wikitext('RP value:') :attr('scope', 'row') :done() :tag('td') :wikitext(rp) if total 0 then return t end t:tag('tr') :tag('th') :wikitext('Average:') :attr('scope', 'row') :done() :tag('td') :wikitext(string.format('%.2f', rp/total)) local breakdown = t:tag('tr') :tag('td') :attr('colspan', 2) :tag('div') :addClass('mw-collapsible mw-collapsed') :attr('data-expandtext', 'show breakdown') :attr('data-collapsetext', 'hide breakdown') :wikitext('\n') for i,v in ipairs(keys(values, true)) do breakdown:wikitext(string.format('* \'\'\'%d\'\'\': %d (%.1f%%)\n', v, valuesv, valuesv/total*100)) end return t end local function rerolls_table(values) local t = mw.html.create('table'):addClass('article-table sortable') t:tag('caption') :wikitext('Recommended rerolls') t:tag('tr') :tag('th') :wikitext('1') :attr('scope', 'col') :css('width', '50px') :done() :tag('th') :wikitext('2') :attr('scope', 'col') :css('width', '50px') :done() :tag('th') :wikitext('3') :attr('scope', 'col') :css('width', '50px') :done() :tag('th') :wikitext('Value') :attr('scope', 'col') :css('width', '150px') for i,v in ipairs(values) do if v4 >= 0 then t:tag('tr') :tag('td') :wikitext(v1) :done() :tag('td') :wikitext(v2) :done() :tag('td') :wikitext(v3) :done() :tag('td') :wikitext(string.format('%.1f', v4)) end end return t end -- Calculations local function rerolls(average) local allskins = copytable(skins) local prices = keys(allskins, true) local res = {} for i = 1, #prices, 1 do local v1 = pricesi if allskinsv1 > 0 then allskinsv1 = allskinsv1 - 1 for j = i, #prices, 1 do local v2 = pricesj if allskinsv2 > 0 then allskinsv2 = allskinsv2 - 1 for k = j, #prices, 1 do local v3 = pricesk if allskinsv3 > 0 then table.insert(res, {v1, v2, v3, formula(average, v1, v2, v3)}) end end allskinsv2 = allskinsv2 + 1 end end allskinsv1 = allskinsv1 + 1 end end return res end -- Invokable local p = {} function p.reroll(frame) local out = mw.html.create() local args = getArgs(frame, {parentFirst=true}) local prices = keys(skins, true) -- Check/prep input values local owned, owned_total, owned_rp = {}, 0, 0 local errors = {} for i,v in ipairs(prices) do ownedv = tonumber(argsv) or 0 -- assume 0 for non-number owned owned_total = owned_total + ownedv owned_rp = owned_rp + ownedv*v if ownedv < 0 or ownedv > skinsv then table.insert(errors, v) end end -- Error message for bad values if #errors > 0 then local res = 'Values out of bounds:' for i,v in ipairs(errors) do res = res .. mw.ustring.format('\n* \'\'\'%d\'\'\' = %d - %d', v, ownedv, skinsv) end return res end out:node(summary_table('Owned skins', owned)):wikitext('\n\n') -- Calculate skins you don't own local missing, missing_total, missing_rp = {}, 0, 0 for i,v in ipairs(prices) do missingv = skinsv - ownedv missing_total = missing_total + missingv missing_rp = missing_rp + missingv*v end out:node(summary_table('Missing skins', missing)):wikitext('\n\n') if missing_total 0 then out:wikitext('You own all skins') else out:node(rerolls_table(rerolls(missing_rp/missing_total))) end return out end function p.usage(frame) local args = getArgs(frame, {frameOnly=true}) local res = '' if args1 then res = ' ' return ' ' .. res .. ' ' end return p -- --Category:Lua